'''
计算两个日期之间的天数(start,end):
思路:
version0.1:
    1. 记days为两个日期之间的天数.初始值为0
    1. start不等于end,则days加1
        1. start为一个元祖(year,month,day)
    1. start获取下一天的日期,重新赋值给start
        1. 先假设每个月只有30天,不考虑2月,瑞年等情况
    1. 重复到2的步骤

version0.2:
    1. 考虑实际的月份天数
    1. 考虑瑞年
'''

def is_leap_year(year):
    '''
    :param date:
    :return:
    '''
    if year %4 == 0:
        if year %100 != 0:
            return True
        if year %400 == 0:
            if year %3200 != 0:
                return True
            return False
        else:
            return False
    else:
        return False

def next_day(now):
    '''
    1. 每个月多少天
    1. 2月考虑是否是瑞年
    :param now:
    :return:
    '''
    month_31 = [1,3,5,7,8,10,12]
    month_30 = [4,6,9,11]
    feb = 2

    if now[1] in month_31:
        if now[2] < 31:
            now[2] += 1
            return now
        now[2] = 1
        if now[1] == 12:
            now[0] += 1
            now[1] = 1
            return now
        now[1] += 1
        return now
    if now[1] in month_30:
        if now[2] < 30:
            now[2] += 1
            return now
        now[2] = 1
        now[1] += 1
        return now
    if now[1] == feb:
        '''先假设每月29天'''
        if is_leap_year(now[0]):
            if now[2] < 28:
                now[2] += 1
                return now
            now[2] = 1
            now[1] += 1
            return now
        if now[2] < 29:
            now[2] += 1
            return now
        now[2] = 1
        now[1] += 1
        return now


def cal_days(start, end):
    '''
    start必须小于end
    :param start:
    :param end:
    :return:
    '''
    if start[0] > end[0]:
        return -1
    if start[0] == end[0] and start[1] > end[1]:
        return -1
    if start[0] == end[0] and start[1] == end[1] and start[2] > end[2]:
        return -1

    days = 0
    while start != end:
        start = next_day(start)
        days += 1
    return days


if __name__ == '__main__':
    assert [2018, 6, 2] == next_day([2018, 6, 1])
    assert [2018, 7, 1] == next_day([2018, 6, 30])
    assert [2018, 7, 31] == next_day([2018, 7, 30])
    assert [2019, 1, 1] != next_day([2018, 12, 30])
    assert [2019, 1, 1] == next_day([2018, 12, 31])
    assert [2018, 4, 1] == next_day([2018, 3, 31])
    assert [2018, 12, 30] == next_day([2018, 12, 29])
    assert [2018, 12, 31] == next_day([2018, 12, 30])
    assert [2019, 1, 1] == next_day([2018, 12, 31])
    assert cal_days([2018,6,30],[2018,6,30]) == 0
    assert cal_days([2018,6,29],[2018,6,30]) == 1
    assert cal_days([2018,6,29],[2018,7,3]) == 4
    assert cal_days([2018,12,29],[2019,1,3]) != 4
    assert cal_days([2018,12,29],[2019,1,3]) == 5
    assert is_leap_year(2018) == False
    assert is_leap_year(401) == False
    assert is_leap_year(2000) == True
    assert is_leap_year(1997) == False
    assert is_leap_year(1900) == False
    assert is_leap_year(1980) == True
    assert cal_days([2000,12,29],[2000,12,30]) == 1
    assert next_day([2000,2,27]) == [2000,2,28]
    assert next_day([2000,2,28]) == [2000,3,1]
    assert cal_days([2000,2,27],[2000,3,3]) == 4
    assert cal_days([2000,3,3],[2000,2,27]) == -1
    print('test passed!'.center(20,'*'))
